<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>艾薇儿</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        #chat-container {
            height: 500px;
            border: 1px solid #ccc;
            overflow-y: auto;
            padding: 20px;
            margin-bottom: 20px;
        }
        .message {
            margin-bottom: 15px;
            padding: 10px;
            border-radius: 5px;
        }
        .user-message {
            background-color: #e3f2fd;
            margin-left: 20%;
        }
        .bot-message {
            background-color: #f5f5f5;
            margin-right: 20%;
        }
        #input-container {
            display: flex;
            gap: 10px;
        }
        #user-input {
            flex-grow: 1;
            padding: 10px;
        }
        button {
            padding: 10px 20px;
            background-color: #2196f3;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #1976d2;
        }
        .loading {
            display: none;
            text-align: center;
            color: #666;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <h1>艾薇儿</h1>
    <div id="chat-container"></div>
    <div class="loading" id="loading">正在思考中...</div>
    <div id="input-container">
        <input type="text" id="user-input" placeholder="请输入您的问题...">
        <button onclick="sendMessage()" id="send-button">发送</button>
    </div>

    <script>
        const chatContainer = document.getElementById('chat-container');
        const userInput = document.getElementById('user-input');
        const sendButton = document.getElementById('send-button');
        const loading = document.getElementById('loading');

        userInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                sendMessage();
            }
        });

        async function sendMessage() {
            const message = userInput.value.trim();
            if (!message) return;

            // 禁用输入和按钮
            userInput.disabled = true;
            sendButton.disabled = true;
            loading.style.display = 'block';

            // 显示用户消息
            appendMessage('user', message);
            userInput.value = '';

            // 准备请求数据
            const messages = [
                {"role": "system", "content": ""},
                {"role": "user", "content": message}
            ];

            try {
                const response = await fetch('http://localhost:9584/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ messages: messages })
                });

                if (!response.ok) {
                    throw new Error('网络响应出错');
                }

                const data = await response.json();
                appendMessage('bot', data.response);
            } catch (error) {
                console.error('Error:', error);
                appendMessage('bot', '抱歉，发生了错误，请稍后重试。');
            } finally {
                // 重新启用输入和按钮
                userInput.disabled = false;
                sendButton.disabled = false;
                loading.style.display = 'none';
                userInput.focus();
            }
        }

        function appendMessage(role, content) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${role}-message`;
            messageDiv.textContent = content;
            chatContainer.appendChild(messageDiv);
            chatContainer.scrollTop = chatContainer.scrollHeight;
        }
    </script>
</body>
</html> 